package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        //获得日期的列表 用stringutils 以 ‘ ，’ 分隔开的字符串
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.isEqual(end)) {
            //begin与end不相等 则添加进集合
            //外部已经是1 所有要加一天再加入集合
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        String dateString = StringUtils.join(dateList, ",");


        //用日期集合查询每日的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            //获得每日的开启和结束时间
            LocalDateTime theDayBegin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime theDayEnd = LocalDateTime.of(localDate, LocalTime.MAX);
            //查询一天的营业额
            Double theDayTurnover =
                    reportMapper.turnoverStatistics(theDayBegin, theDayEnd, Orders.COMPLETED);
            //添加
            turnoverList.add(theDayTurnover);
        }
        String turnoverString = StringUtils.join(turnoverList, ",");


        //返回数据
        return TurnoverReportVO.builder()
                .dateList(dateString)
                .turnoverList(turnoverString)
                .build();


    }
}
